package yl.hs.bmipfull.service.production.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.production.IProductionPackageDao;
import yl.hs.bmipfull.daos.production.IProductionPackageUserDao;
import yl.hs.bmipfull.pojo.production.ProductionPackageEntity;
import yl.hs.bmipfull.service.production.IProductionPackageService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.ProductPackModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 作用:  服务层接口实现<br/>
 * 创建时间: 2023年07月17日 10时40分29秒 <br/>
 *
 * @author 吴高丰
 */
@Service
public class ProductionPackageServiceImpl extends ProductionBaseService implements IProductionPackageService {

    @Autowired
    IProductionPackageDao dao;
    @Autowired
    IProductionPackageUserDao userDao;

    @Override
    public List<ProductionPackageEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ProductionPackageEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<Object> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(ProductionPackageEntity.class);
        var user = HsApp.getCurrent();
        if(user!=null){
            pageQuery.getQuery().put("types", user.getType()==null?0:user.getType());
        }
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<Object> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        var data = dao.queryPage(pageQuery.getQuery());
        List<Object> list = new ArrayList<>();
        data.forEach(t->{
            var map = t.toMap();
            map.put("detail",userDao.queryByPackSNList(t.getSn()));
            list.add(map);
        });
        result.setData(list);
        return result;
    }


    @Override
    public ProductionPackageEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveproduction_package(ProductPackModel model) throws Exception {
        var result = 0;
        var list = model.getData();
        list.forEach(s->{
            if(s.getSn()!=null && s.getSn()!=""){
                var old = dao.findBySN(s.getSn());
                writeLog(ActionType.UPDATE, s.getSn(), "更新信息,库存数量由"+old.getQuantity()+"变为"+s.getQuantity(), Tl.toJson(s), ProductionPackageEntity.class);
                dao.update(s);
            }else{
                s.setSn(queryNewSN(ProductionPackageEntity.class));
                dao.insertNew(s);
                writeLog(ActionType.CREATE, s.getSn(), "插入信息，原始库存"+s.getQuantity(), Tl.toJson(s), ProductionPackageEntity.class);
            }
        });
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, ProductionPackageEntity.class);
        }
        return result;
    }

    @Override
    public List<ProductionPackageEntity> queryByProductSN(String productSN) {
        return dao.queryByProductSN(productSN);
    }
}